<%@ page language="java" import="cn.edu.cuit.elena.transaction.system.UserContext" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
			String basePath = request.getScheme() + "://"
					+ request.getServerName() + ":" + request.getServerPort()
					+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<head>
<base href="<%=basePath%>">
<title>user_management</title>
<link rel="stylesheet" type="text/css"
	href="js/ext/resources/css/ext-all.css" />
<link rel="stylesheet" type="text/css" href="style/mystyle.css" />
<script type="text/javascript" src="js/ext/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="js/ext/ext-all.js"></script>
<script>
	Ext.onReady(function() {
	
			var b_username = "null";
			var b_url= "null" ;
			var b_password= "null" ;
			var b_driver= "null" ;
			var b_schemaname = "null";
			var flag;
	
	var dbcombo = new Ext.form.ComboBox({
			　	triggerAction:'all',
　				mode:'local',
				id:'database',//////////////////////////////////////////////////////////// vender
				fieldLabel : '数据库类型',
				name : 'name',
				xtype : 'combo',
				width:300,
				store : new Ext.data.SimpleStore({
					fields : ['text', 'value'],
					data : [['Oracle  ORACLE DATABASE 11g','1'],
							['Oracle  ORACLE DATABASE 10g','2'],
							['Oracle  ORACLE DATABASE 9i','3'],
							['Oracle  MySql 5.0.22','4'],
							['Oracle  MySql 5.2.20a','5'],
							['Oracle  MySql 4.*','6'],
							['Mircrosoft  Sql Server 2008','7'],
							['Mircrosoft  Sql Server 2005','8'],
							['Mircrosoft  Sql Server 2000','9'],
							['IBM  DB2 9.*','10'],
							['IBM  DB2 8.*','11'],
							['IBM  DB2 7.*','12'],
							['Sy  Base  *','13']
							]
				// from states.js  
				}),
				valueField:'value',
　				displayField:'text'
			});
		var profilePanel = new Ext.FormPanel({
			frame : true,
			region : 'center',
			margins : '5 0 5 5',
			//bodyStyle:'background:#f1f1f1',
			width : 700,
			items : [dbcombo, {
				fieldLabel : '实例名',
				xtype : 'textfield',
				name : 'name',
				width:300,
				id:'name'
			}, {
				fieldLabel : '用户名',
				xtype : 'textfield',
				name : 'username',
				width:300,
				id:'username'
			}, {
				fieldLabel : '密码',
				name : 'password',
				xtype : 'textfield',
				width:300,
				id:'password'
			}, {
				fieldLabel : '链接URL字符串',
				name : 'url',
				xtype : 'textfield',
				width:300,
				id:'url'
			}]
		});
		
		
		/////////////////////////////////////////////////////////////buttons
		var buttons = new Ext.Panel({
			frame:true,
			region:'east',
			margins:'5 0 5 5',
			width:115,
			items:[ 
			new Ext.Button({
			text:"添加配置",
			width:100,
				height:40,
				handler:function(){
					var username = Ext.getCmp("username").getValue();
					var password = Ext.getCmp("password").getValue();
					var url = Ext.getCmp("url").getValue();
					var name = Ext.getCmp("name").getValue();
					var database = Ext.getCmp("database").getValue();
					
					
					if(username != "" &&password != ""&&url!=""&&name!=""&&database!=""){
					var requestConfig = {
						url:'datasourcemanagement.svt?kind=add',
						params:{username:username,password:password,url:url,name:name,database:database},
						callback:function(option,success,response){
							if(response.responseText == "1"){
							dataSourcePanel.getStore().reload();
								Ext.MessageBox.alert("Prompt","配置成功!");
								dataSourcePanel.getStore().reload();
							}else{
							Ext.MessageBox.alert("Prompt","添加失败!");
							}
						}
					}
					 Ext.Ajax.request(requestConfig);
					}else{
										Ext.MessageBox.alert('Prompt', 'Please configurate data source!');
					}
				}
			})
			, new Ext.Button({
			text:"废弃数据源",
			width:100,
				height:40,
				handler:function(){
				
				if(b_username != "null" &&b_url != "null"&&b_password!="null"&&b_driver!="null"){
					Ext.MessageBox.confirm("Warn!","是否却确认废弃数据源!",function(button,text){
						if(button == "no"){
						return;
					 }
					 
						var requestConfig = {
						url:'datasourcemanagement.svt?kind=delete',
						params:{b_username:b_username,b_url:b_url,b_password:b_password,b_driver:b_driver},
						callback:function(option,success,response){
							if(response.responseText == "1"){
								Ext.MessageBox.alert("Prompt","成功!");
							}else{
							Ext.MessageBox.alert("Prompt","失败!");
							}
							dataSourcePanel.getStore().reload();
						}
					}
					 Ext.Ajax.request(requestConfig);
					})
					}else{
										Ext.MessageBox.alert('Prompt', 'Please choise a source!');
					}
					}
			}), new Ext.Button({
				text:"添加到迁移任务",
			width:100,
				height:40,
				handler:function(){
				if(b_username != "null" &&b_url != "null"&&b_password!="null"&&b_driver!="null"){
					Ext.MessageBox.confirm("Prompt!","请确认时否要添加迁移!",function(button,text){
						if(button == "no"){
						return;
					 }
					 
						var requestConfig = {
						url:'datasourcemanagement.svt?kind=migration',
						params:{b_username:b_username,b_url:b_url,b_password:b_password,b_driver:b_driver},
						callback:function(option,success,response){
							if(response.responseText == "1"){
								Ext.MessageBox.alert("Prompt","添加到迁移面板!");
							}else if(response.responseText == "2"){
							Ext.MessageBox.alert("Prompt","请不要重复添加!");
							}else{
							Ext.MessageBox.alert("Prompt","失败!");
							}
						}
					}
					 Ext.Ajax.request(requestConfig);
					})
					}else{
										Ext.MessageBox.alert('Prompt', 'Please choise a source!');
					}
					}			}),new Ext.Button({
			text:"重置",
			width:100,
				height:40,
				handler:function(){
					Ext.getCmp("username").setValue("");
					Ext.getCmp("password").setValue("");
					Ext.getCmp("url").setValue("");
					Ext.getCmp("name").setValue("");
					Ext.getCmp("database").setValue("");
				}
			})]
		});
		
		var container_2 = new Ext.Panel({
			title:'数据源配置',
			width:650,
			layout : 'border',
			frame:true,
			region:'east',
			margins:'5 0 5 5',
			items:[profilePanel,buttons]
		});

		var editPanel = new Ext.Panel({
			title:"欢迎使用",
			frame : true,
			region : 'center',
			margins : '5 5 5 5',
			//bodyStyle:'background:#f1f1f1',
			items:[
				{
					html:'用户：<%=((UserContext) session.getAttribute("usercontext")).getIdentifier()%>',
				},
				{
				html:'欢迎使用数据迁移平台'
				},{
				html:'请编辑并配置数据源，添加迁移任务'
				}]
		});

		var c_panel = new Ext.Panel({
			frame : true,
			layout : 'border',
			region : 'north',
			height : 250,
			items : [container_2, editPanel]
		});

		var store = new Ext.data.ArrayStore({
			fields : [{
				name : 'company'
			}, {
				name : 'price',
				type : 'float'
			}, {
				name : 'change',
				type : 'float'
			}, {
				name : 'pctChange',
				type : 'float'
			}, {
				name : 'lastChange',
				type : 'date',
				dateFormat : 'n/j h:ia'
			}]
		});

	//////////////////////////////////////////////////////////////////datasource grid
	
		var proxy = new Ext.data.HttpProxy({
			url : 'datasourcemanagement.svt?kind=loaddatasource'
		});
		//定义reader
		var reader = new Ext.data.JsonReader({}, [{
			name : 'userid'
		}, {
			name : 'username'
		}, {
			name : 'password'
		}, {
			name : 'url'
		},{
			name:'datasourceid'
		},{
			name:'vender'
		},{
			name:'schemaname',
		},{
			name:'state'
		},{
			name:'driver'
		}]);
		//构建Store   
		var store = new Ext.data.Store({
			proxy : proxy,
			reader : reader
		});
		//载入
		store.load();
		
		
		
		

		var dataSourcePanel = new Ext.grid.GridPanel({
			store:store,
			frame : true,
			region : 'center',
			store : store,
			columns : [
// 			{
// 				id : 'Data Source Id',
// 				header : 'datasourceid',
// 				width : 160,
// 				sortable : true,
// 				dataIndex : 'datasourceid'
// 			},
			{
				id : 'User Name',
				header : 'username',
				width : 160,
				sortable : true,
				dataIndex : 'username'
			},{
				id : 'Password',
				header : 'password',
				width : 160,
				sortable : true,
				dataIndex : 'password'
			},{
				id : 'Connection URL',
				header : 'url',
				width : 160,
				sortable : true,
				dataIndex : 'url'
			},{
				id : 'Driver',
				header : 'driver',
				width : 160,
				sortable : true,
				dataIndex :'driver'
			},{
				id : 'Edtition Detail',
				header : 'vender',
				width : 160,
				sortable : true,
				dataIndex : 'vender'
			},{
				id : 'Instance Name',
				header : 'schemaname',
				width : 160,
				sortable : true,
				dataIndex : 'schemaname'
			},{
				id : 'State',
				header : 'state',
				width : 160,
				sortable : true,
				dataIndex : 'state'
			}],
			stripeRows : true,
			title : 'Data Source Details',
			// config options for stateful behavior
			stateful : true,
			stateId : 'grid'
		});
		

		var viewport = new Ext.Viewport({
			layout : 'border',
			items : [c_panel, dataSourcePanel]
		});
		
		
			/***********************************************
			              Listeners
		 ***********************************************/
		dataSourcePanel.addListener('cellclick', cellclick);

		function cellclick(grid, rowIndex, columnIndex, e) {
			var record = grid.getStore().getAt(rowIndex); //Get the Record   
			var selectRows = grid.getSelectionModel().getSelections();
			b_username = selectRows[0].get("username");
			b_url = selectRows[0].get("url");
			b_password = selectRows[0].get("password");
			b_driver = selectRows[0].get("driver");
			b_schemaname = selectRows[0].get("schemaname");
			
			
			
			// 			Ext.MessageBox.alert('show', '当前选中的数据是' + username + gender
			// 					+ usergroup + comments);
		}
	
		
	});
	
</script>
</head>
<body>
</body>
</html>
